package cn.laixueit.cart.controller;

import cn.laixueit.cart.pojo.item.SpecGroup;
import cn.laixueit.cart.service.SpecGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashMap;

@Controller
@RequestMapping("/spec")
public class SpecGroupController {

    @Autowired
    private SpecGroupService specGroupService;

    @GetMapping("/groups/{cid}")
    public ResponseEntity<Collection<SpecGroup>> groups(@PathVariable("cid")Long cid){
        HashMap<String, Object> map = new HashMap<>();
        map.put("cid",cid);
        Collection<SpecGroup> groups = specGroupService.listByMap(map);
        return ResponseEntity.ok(groups);
    }

    @PostMapping("/group")
    public ResponseEntity<Void> save(@RequestBody SpecGroup specGroup){

        specGroupService.saveOrUpdate(specGroup);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("/group/{id}")
    public ResponseEntity<Void> del(@PathVariable("id")Long id){

        specGroupService.removeById(id);

        return ResponseEntity.status(HttpStatus.CREATED).build();

    }
}
